#pragma once

#include <iostream>
#include <string>
#include <cstdarg>
#include <ctime>
#include <unistd.h>
#include <fstream>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

enum
{
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal
};
enum
{
    Screen = 10,
    ClassFile,
    OneFile
};

std::string LevelToString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
    case Info:
        return "Info";
    case Warning:
        return "Warning";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "Unknown";
    }
}

const int defaultstyle = Screen;
const std::string default_filename = "log.";
const std::string logdir = "log";
class Log
{
public:
    Log():style(defaultstyle),filename(default_filename) {
        mkdir(logdir.c_str(),0775);
    }
    void Enable(int sty){
        style = sty;
    }
    std::string TimeStampExlocaltime(){// 时间戳转化成字符串
        time_t currtime = time(nullptr);
        struct tm *curr =  localtime(&currtime);
        char time_buffer[128];
        snprintf(time_buffer,sizeof(time_buffer),"%d-%d-%d %d:%d:%d",\
        curr-> tm_year + 1900,curr->tm_mon + 1,curr->tm_mday,\
        curr->tm_hour,curr->tm_min,curr->tm_sec);
        return time_buffer;
    }
    void WriteLogOneFile(const std::string logname, const std::string &message)
    {
        umask(0);
        int fd = open(logname.c_str(),O_CREAT | O_WRONLY | O_APPEND);
        if(fd < 0)return ;
        write(fd,message.c_str(), message.size());
        close(fd);
        // std::ofstream out(logname);
        // if(!out.is_open())
        //     return;
        // out.write(message.c_str(),message.size());
        // out.close();
    }
    void WriteLogClassFile(const std::string leverstr, const std::string &message)
    {
        std::string logname = logdir;
        logname += "/";
        logname += filename;
        logname += leverstr;
        WriteLogOneFile(logname,message);
    }
    void WriteLog(const std::string &leverstr, const std::string &message)
        {
            switch (style)
            {
            case Screen:
                std::cout << message;
                break;
            case OneFile:
                WriteLogOneFile("all", message);
                break;
            case ClassFile:
                WriteLogClassFile(leverstr,message);
                break;
            default:
                break;
            }
        }
    void LogMessage(int level, const char *format, ...) // 类C的一个日志接口
    {
        // char content[1024];
        // va_list args; // char *, void *
        // va_start(args, format);
        // // args 指向了可变参数部分
        // vsnprintf(content, sizeof(content), format, args);
        // va_end(args); // args = nullptr;
        // // uint64_t currtime = time(nullptr);

        // std::string currtime = TimeStampExlocaltime();
        // printf("[%s][%s]%s\n", LevelToString(level).c_str(),
        //        std::to_string(currtime).c_str(), content);

        char rightbufer[1024];
        va_list args;
        va_start(args,format);
        vsnprintf(rightbufer,sizeof(rightbufer),format,args);
        va_end(args);

        char leftbuffer[1024];
        std::string leverstr = LevelToString(level);
        std::string currtime = TimeStampExlocaltime();
        std::string id_str =std::to_string(getpid());
        
        snprintf(leftbuffer,sizeof(leftbuffer),"[%s][%s][%s]",
            leverstr.c_str(),currtime.c_str(),id_str.c_str());

        std::string loginfo = leftbuffer;
        loginfo += rightbufer;
        WriteLog(leverstr,loginfo);
    }
    ~Log() {}

private:
    int style;
    std::string filename;
};
Log lg;
class Conf{
public:
    Conf(){
        lg.Enable(Screen);       
    }
    ~Conf(){}
private:
};
